
var express = require('express');
var query = require('../connent/mysqlPool') // 引入数据库连接池文件
const multerConfig = require('./multerConfig');
let format = require('./format');
const { log } = require('debug/src/browser');
var router = express.Router();

router.get('/getcar', (req, res) => {
  query("select * from vehicle", (err, result, fields) => {
    res.send({
      code: 1,
      data: entryTimeSort(result)
    })
  })
})


/* 时间排序 */
let entryTimeSort=(data)=>{
  let carInfo = data.map((item, index) => {
    item.entry_time = new Date(item.entry_time).getTime()
    return item
  })
  carInfo.sort((x, y) => {
    return y.entry_time - x.entry_time
  })

  let result = carInfo.map(item=>{
    item.entry_time = new Date(item.entry_time).format("yyyy-MM-dd hh:mm:ss");
    return item
  })
  return result
}

/* 分页获取车辆数据 */
router.get('/paggetcar', (req, res) => {
  let params = req.query
  let current = parseInt(params.current)
  let pageSize = parseInt(params.pageSize)
  let page = parseInt(params.page)||0
  let startLocation = (current - 1) * pageSize
  let pageSql = "select * from vehicle limit ?,?"
  if(page!==0){  //判断导出页范围
    pageSize = pageSize * page
  }
  query(pageSql, [startLocation,pageSize], (err, result, fields) => {
    if (err) {
      console.log('[SELECT ERROR]:', err.message)
    }
    res.send({
      data: entryTimeSort(result)
    }) // 服务器响应请求
  })
})

/* 车辆图片上传 */
const fileName = "file"
const updateBaseUrl = "http://localhost:5000"
const imgPath = "/images/"

function upload(req, res) {
  return new Promise((resolve, reject) => {
    multerConfig.single(fileName)(req, res, function (err) {
      if (err) {
        reject(err)
      } else {
        resolve(updateBaseUrl + imgPath + req.file.filename)
      }
    });
  })
}
router.post('/imgupload', (req, res) => {
  upload(req, res).then(imgsrc => {
    res.send({
      data: imgsrc
    })
  })
})



/* 添加车辆数据 */
router.post('/addcar', (req, res) => {
  let params = req.body
  let carInfo = []
  carInfo.push(params.plate_number)
  carInfo.push(params.vehicle_color)
  carInfo.push("白色")
  carInfo.push("大众-帕萨特")
  carInfo.push(params.point)
  carInfo.push(0)
  carInfo.push(1)
  carInfo.push(params.vehicle_img)
  carInfo.push(params.entry_time)
  let addCarSql = "insert into vehicle(plate_number,vehicle_color,plate_color,vehicle_type,point,skylight,roof_rack,vehicle_img,entry_time) values(?,?,?,?,?,?,?,?,?)"
  query(addCarSql, carInfo, (err, result, fields) => {
    res.send({
      message: "添加成功"
    })
  })
})


/* 车辆搜索*/
router.post('/carsearch', (req, res) => {
  console.log(req.body);
  let searchCondition = req.body;
  let plate_number = searchCondition.plate_number
  let plate_color = searchCondition.plate_color
  let startTime = searchCondition.startTime
  let endTime = searchCondition.endTime
  let searchSql =""
  let param = []
  if (plate_number === "" && plate_color===""){
    searchSql = "select * from vehicle where entry_time >= ? and entry_time <= ?"
    param = [startTime, endTime]
  } else if (plate_number === "" && plate_color !== ""){
    searchSql = "select * from vehicle where plate_color=? and entry_time >= ? and entry_time <= ?"
    param = [plate_color,startTime, endTime]
  } else if (plate_number !== "" && plate_color == "") {
    searchSql = "select * from vehicle where plate_number=? and entry_time >= ? and entry_time <= ?"
    param = [plate_number, startTime, endTime]
  }else{
    searchSql = "select * from vehicle where plate_number=? and plate_color=? and entry_time >= ? and entry_time <= ?"
    param = [plate_number, plate_color, startTime, endTime]
  }
  query(searchSql,param , (err, result, fields) => {
    res.send({
      message: "查询成功",
      data: entryTimeSort(result)
    })
  })
})

/* 分组查询 */
router.get('/cargroupsearch', (req, res) => { 
  console.log(req.query);
  let searchCondition = req.query.group
  let carGroupSql =""
  if(searchCondition==="不分组"){
    carGroupSql = "select * from vehicle limit 0,12"
  } else if (searchCondition === "按照车牌分组"){
    carGroupSql = `select plate_number,count(*) as count from vehicle group by plate_number`
  } else if (searchCondition === "按照点位分组"){
    carGroupSql = `select point,count(*) as count from vehicle group by point`
  } else if (searchCondition === "按照车辆品牌分组"){
    carGroupSql = `select  vehicle_type,count(*) as count from vehicle group by vehicle_type`
  }
  query(carGroupSql, (err, result, fields) => {
    if(searchCondition==="不分组"){
      result = entryTimeSort(result)
      res.send({
        code: 0,
        data: result
      })
    }else{
      res.send({
        code: 1,
        group: searchCondition,
        data: result
      })
    }
  })
})

/* 删除 */
router.get('/deletecar', (req, res) => {
  let id = req.query.id
  query("delete from vehicle where id=?",[id], (err, result, fields) => {
    res.send({
      code:1,
      message:"删除成功"
    })
  })
})

module.exports = router